package com.yulongtian.interviewWorks.day20220825;

import java.util.LinkedList;

/**
 * https://leetcode.cn/problems/sum-lists-lcci/
 *
 * @author YulongTian
 * @date 2022/8/25 - 13:44
 */
public class Test01 {

    public static void main(String[] args) {

    }

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode p1 = l1;
        ListNode pre1 = l1;
        ListNode p2 = l2;
        ListNode pre2 = l2;

        int c = 0;//进位

        while (p1 != null || p2 != null || c != 0) {
            if (p1 == null) {
                p1 = new ListNode(0);
                pre1.next = p1;
            }
            if (p2 == null) {
                p2 = new ListNode(0);
                pre2.next = p2;
            }
            int num = p1.val + p2.val + c;

            p1.val = num % 10;
            c = num / 10;

            pre1 = p1;
            p1 = p1.next;
            pre2 = p2;
            p2 = p2.next;
        }


        return l1;
    }

    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }
}
